package net.risesoft.controller.resource;

import java.util.List;

import javax.validation.constraints.NotBlank;

import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;

import net.risesoft.enums.platform.org.ManagerLevelEnum;
import net.risesoft.log.OperationTypeEnum;
import net.risesoft.log.annotation.RiseLog;
import net.risesoft.model.platform.System;
import net.risesoft.permission.annotation.IsAnyManager;
import net.risesoft.pojo.Y9Result;
import net.risesoft.vo.resource.SystemTreeNodeVO;
import net.risesoft.y9.Y9LoginUserHolder;
import net.risesoft.y9public.service.resource.Y9SystemService;
import net.risesoft.y9public.service.tenant.Y9TenantSystemService;

/**
 * 系统管理
 *
 * @author shidaobang
 * @date 2022/3/2
 * @since 9.6.0
 */
@Validated
@RestController
@RequestMapping(value = "/api/rest/system", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
@IsAnyManager({ManagerLevelEnum.SYSTEM_MANAGER, ManagerLevelEnum.OPERATION_SYSTEM_MANAGER,
    ManagerLevelEnum.SECURITY_MANAGER})
public class SystemController {

    private final Y9SystemService y9SystemService;
    private final Y9TenantSystemService y9TenantSystemService;

    /**
     * 删除系统
     *
     * @param id 系统id
     * @return {@code Y9Result<Object>}
     */
    @RiseLog(operationName = "删除系统")
    @PostMapping(value = "/delete")
    public Y9Result<Object> delete(@RequestParam @NotBlank String id) {
        y9SystemService.delete(id);
        y9TenantSystemService.deleteByTenantIdAndSystemId(Y9LoginUserHolder.getTenantId(), id);
        return Y9Result.successMsg("删除系统成功");
    }

    /**
     * 禁用系统
     *
     * @param id 系统id
     * @return {@code Y9Result<}{@link System}{@code >}
     */
    @RiseLog(operationName = "禁用系统")
    @PostMapping(value = "/disable")
    public Y9Result<System> disable(@RequestParam @NotBlank String id) {
        return Y9Result.success(y9SystemService.disable(id), "禁用系统成功");
    }

    /**
     * 启用系统
     *
     * @param id 系统id
     * @return {@code Y9Result<}{@link System}{@code >}
     */
    @RiseLog(operationName = "启用系统")
    @PostMapping(value = "/enable")
    public Y9Result<System> enable(@RequestParam @NotBlank String id) {
        return Y9Result.success(y9SystemService.enable(id), "启用系统成功");
    }

    /**
     * 根据系统id获取系统详情
     *
     * @param id 系统id
     * @return {@code Y9Result<}{@link System}{@code >}
     */
    @RiseLog(operationName = "根据系统id获取系统详情")
    @GetMapping(value = "/{id}")
    public Y9Result<System> getById(@PathVariable @NotBlank String id) {
        return Y9Result.success(y9SystemService.getById(id), "根据系统id获取系统详情成功");
    }

    /**
     * 获取系统列表
     *
     * @return {@code Y9Result<List<}{@link SystemTreeNodeVO}{@code >>}
     */
    @RiseLog(operationName = "获取系统列表")
    @GetMapping(value = "/list2")
    public Y9Result<List<SystemTreeNodeVO>> list2() {
        List<System> systemList;
        if (ManagerLevelEnum.OPERATION_SYSTEM_MANAGER.equals(Y9LoginUserHolder.getUserInfo().getManagerLevel())) {
            systemList = y9SystemService.listAll();
        } else {
            systemList = y9TenantSystemService.listSystemByTenantId(Y9LoginUserHolder.getTenantId());
        }
        return Y9Result.success(SystemTreeNodeVO.convertY9SystemList(systemList), "获取系统列表成功");
    }

    /**
     * 保存系统
     *
     * @param system 系统
     * @return {@code Y9Result<}{@link System}{@code >}
     */
    @RiseLog(operationName = "保存系统")
    @PostMapping(value = "/save")
    public Y9Result<System> save(@Validated System system) {
        return Y9Result.success(y9SystemService.saveAndRegister4Tenant(system), "保存系统成功");
    }

    /**
     * 保存系统排序
     *
     * @param systemIds 系统id数组
     * @return {@code Y9Result<String>}
     */
    @RiseLog(operationName = "保存系统排序", operationType = OperationTypeEnum.MODIFY)
    @PostMapping(value = "/saveOrder")
    public Y9Result<String> saveOrder(@RequestParam String[] systemIds) {
        y9SystemService.saveOrder(systemIds);
        return Y9Result.success(null, "系统排序成功！");
    }
}